package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecParamService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {
    @Override
    public List<SpecParamDTO> queryParam(Long categoryId, Long groupId, Boolean searching) {
        if(categoryId==null && groupId==null){
            throw new LyException(400,"必须包含一个id查询条件");
        }
        List<SpecParam> paramList = query()
                .eq(categoryId != null, "category_id", categoryId)
                .eq(groupId != null, "group_id", groupId)
                .eq(searching != null, "searching", searching)
                .list();
        return SpecParamDTO.convertEntityList(paramList);
    }

    @Override
    public void saveParam(SpecParamDTO body) {
        save(body.toEntity(SpecParam.class));
    }

    @Override
    public void updateParam(SpecParamDTO body) {
        updateById(body.toEntity(SpecParam.class));
    }
}
